﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Impl;
using Ones.Infrastructure.Utils.Pagination.Impl;

namespace Ones.Wcf.WebAdministration
{
    public class ServiceCommentManagement : IServiceCommentManagement
    {
        private ICommentDao _dao;
        
        #region Constructor
 
        public ServiceCommentManagement()
        {
            _dao = new CommentDao();
        }

        #endregion

        #region DMM
        
        public bool
        writeComment(Comment cmt)
        {
            return _dao.create(cmt);
        }
        
        public bool
        updateComment(Comment cmt)
        {
            return _dao.update(cmt);
        }
        
        public bool
        deleteComment(int cmtId)
        {
            return _dao.delete(cmtId);
        }

        public bool
        allowComment(int cmtId)
        {
            var curr = _dao.getById(cmtId);

            curr.cmtIsIllegal = false;

            return _dao.update(curr);
        }
        
        public bool
        disallowComment(int cmtId)
        {
            var curr = _dao.getById(cmtId);

            curr.cmtIsIllegal = true;

            return _dao.update(curr);
        }
        
        #endregion
        
        #region DQM
        
        public List<Comment>
        searchAllComments()
        {
            return _dao.getAll();
        }
        
        public List<Comment>
        searchCommentByNews(int id)
        {
            return _dao.getByNews(id);   
        }
        
        public List<Comment>
        searchCommentByIllegal(bool isIllegal)
        {
            return _dao.getByIllegal(isIllegal);
        }

        public List<Comment>
        searchAllLatestComments()
        {
            var lst = _dao.getAll();

            if (lst != null)
            {
                lst = lst.OrderByDescending(i => i.cmtDatePost).ToList();
            }

            return lst;
        }

        public List<Comment>
        searchLatestCommentByNews(int id)
        {
            var lst = _dao.getByNews(id);

            if (lst != null)
            {
                lst = lst.OrderByDescending(i => i.cmtDatePost).ToList();
            }

            return lst;
        }

        public List<Comment>
        searchLatestCommentByIllegal(bool isIllegal)
        {
            var lst = _dao.getByIllegal(isIllegal);

            if (lst != null)
            {
                lst = lst.OrderByDescending(i => i.cmtDatePost).ToList();
            }

            return lst;
        }

        #endregion
        
        #region ///Paging///
        
        public LazyPagination<Comment>
        pagingAllComments(int pageNumber, int pageSize)
        {
            var lst = _dao.getAll().AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }

        public LazyPagination<Comment>
        pagingCommentByNews(int id, int pageNumber, int pageSize)
        {
            var lst = _dao.getMultiple(i => i.newsId == id).AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }

        public LazyPagination<Comment>
        pagingCommentByIllegal(bool isIllegal, int pageNumber, int pageSize)
        {
            var lst = _dao.getMultiple(i => i.cmtIsIllegal == isIllegal).AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }

        public LazyPagination<Comment>
        pagingAllLatestComments(int pageNumber, int pageSize)
        {
            var lst = _dao.getAll()
                          .OrderByDescending(i => i.cmtDatePost)
                          .AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }

        public LazyPagination<Comment>
        pagingLatestCommentByNews(int id, int pageNumber, int pageSize)
        {
            var lst = _dao.getMultiple(i => i.newsId == id)
                          .OrderByDescending(i => i.cmtDatePost)
                          .AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }

        public LazyPagination<Comment>
        pagingLatestCommentByIllegal(bool isIllegal, int pageNumber, int pageSize)
        {
            var lst = _dao.getMultiple(i => i.cmtIsIllegal == isIllegal)
                          .OrderByDescending(i => i.cmtDatePost)
                          .AsQueryable();
            return new LazyPagination<Comment>(lst, pageNumber, pageSize);
        }
        
        #endregion
    }
}